<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="org.example.entity.Person" %>
<%@ page import="java.util.List" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>投票系统 - 用户查询</title>
    <!-- 引入外部资源 -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.8.17/css/layui.min.css">
    <script src="../../js/layui.js"></script>
    <script src="../../js/jquery-3.7.1.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <style>
        /* 复用原页面基础样式 */
        body {
            background-color: #f8f9fa;
            margin: 0;
            padding: 0;
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            color: #333;
        }

        .action-buttons {
            display: flex;
            gap: 8px;
        }

        .action-buttons button {
            background-color: transparent;
            border: none;
            cursor: pointer;
            padding: 5px;
            border-radius: 5px;
            transition: all 0.3s ease;
        }

        .action-buttons .edit-btn {
            color: #1E9FFF;
        }

        .action-buttons .edit-btn:hover {
            background-color: rgba(30, 159, 255, 0.1);
        }

        .action-buttons .delete-btn {
            color: #dc3545;
        }

        .action-buttons .delete-btn:hover {
            background-color: rgba(220, 53, 69, 0.1);
        }

        .header {
            background-color: #ffffff;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            padding: 15px 30px;
            display: flex;
            align-items: center;
        }

        .header a {
            color: #333;
            margin-left: 25px;
            text-decoration: none;
            transition: color 0.3s ease;
            font-size: 15px;
        }

        .header a:hover {
            color: #1E9FFF;
        }

        .header > a:first-child {
            margin-left: 0;
        }

        .ml-auto {
            margin-left: auto;
        }

        .admin-container {
            padding: 30px 20px;
            max-width: 1400px;
            margin: 0 auto;
        }

        .layui-tab {
            margin: 0;
            background-color: #ffffff;
            border-radius: 16px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
            overflow: hidden;
        }

        .module-header {
            padding: 25px 30px 15px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #f0f2f5;
        }

        .module-title {
            font-size: 20px;
            font-weight: 600;
            color: #333;
        }

        .module-actions {
            display: flex;
            gap: 10px;
        }

        .module-actions button {
            background-color: #1E9FFF;
            color: white;
            border: none;
            padding: 10px 18px;
            border-radius: 8px;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            font-size: 14px;
            font-weight: 500;
            box-shadow: 0 2px 8px rgba(30, 159, 255, 0.2);
        }

        .module-actions button:hover {
            background-color: #0d87e6;
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(30, 159, 255, 0.3);
        }

        .module-actions button i {
            margin-right: 6px;
        }

        .module-actions .reset-btn {
            background-color: #6c757d;
            box-shadow: 0 2px 8px rgba(108, 117, 125, 0.2);
        }

        .module-actions .reset-btn:hover {
            background-color: #5a6268;
        }

        /* 查询表单样式 */
        .search-form {
            padding: 25px 30px;
            border-bottom: 1px solid #f0f2f5;
        }

        .form-row {
            display: flex;
            flex-wrap: wrap;
            gap: 20px;
            margin-bottom: 20px;
        }

        .form-group {
            flex: 1;
            min-width: 200px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            color: #666;
            font-weight: 500;
        }

        .layui-input, .layui-select, .layui-textarea {
            width: 100%;
            padding: 10px 14px;
            border-radius: 8px;
            border: 1px solid #e0e2e5;
            font-size: 14px;
            transition: all 0.2s ease;
        }

        .layui-input:focus, .layui-select:focus, .layui-textarea:focus {
            border-color: #1E9FFF;
            box-shadow: 0 0 0 3px rgba(30, 159, 255, 0.1);
        }

        .date-range {
            display: flex;
            gap: 10px;
        }

        .date-range .layui-input {
            flex: 1;
        }

        .date-separator {
            display: flex;
            align-items: center;
            color: #999;
        }

        /* 表格样式 */
        .admin-table {
            width: 100%;
            border-collapse: collapse;
            margin: 30px;
        }

        .admin-table th, .admin-table td {
            padding: 18px 20px;
            text-align: left;
            border-bottom: 1px solid #f0f2f5;
        }

        .admin-table th {
            background-color: #f8f9fa;
            color: #666;
            font-weight: 500;
            font-size: 14px;
            position: relative;
        }

        .admin-table tr:last-child td {
            border-bottom: none;
        }

        .admin-table tr:hover {
            background-color: #fefefe;
        }

        /* 分页样式 */
        .pagination-container {
            display: flex;
            justify-content: center;
            margin: 30px 0 20px;
        }

        .pagination {
            display: inline-flex;
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .pagination li {
            margin: 0 4px;
        }

        .pagination li a {
            display: inline-block;
            padding: 10px 16px;
            border-radius: 8px;
            text-decoration: none;
            color: #666;
            background-color: #f0f2f5;
            transition: all 0.2s ease;
            font-size: 14px;
            font-weight: 500;
        }

        .pagination li a:hover,
        .pagination li.active a {
            background-color: #1E9FFF;
            color: #fff;
            box-shadow: 0 2px 8px rgba(30, 159, 255, 0.2);
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .admin-container {
                padding: 20px 15px;
            }

            .search-form, .admin-table {
                padding: 15px;
            }

            .form-row {
                gap: 15px;
            }

            .admin-table th, .admin-table td {
                padding: 15px 12px;
                font-size: 13px;
            }

            .pagination li a {
                padding: 8px 12px;
                font-size: 13px;
            }
        }

        @media (max-width: 480px) {
            .form-row {
                flex-direction: column;
                gap: 15px;
            }

            .module-actions {
                flex-direction: column;
                gap: 8px;
            }

            .date-range {
                flex-direction: column;
            }

            .date-separator {
                display: none;
            }
        }
    </style>
</head>
<body>
<nav class="header">

    <c:if test="${person.level == 1}">
        <a href="${pageContext.request.contextPath}/person/index" class="flex items-center">
            <i class="fa fa-home text-primary text-xl mr-2"></i>
            <span class="font-bold text-lg">用户首页</span>
        </a>
        <a href="${pageContext.request.contextPath}/admin/index" class="flex items-center">
            <i class="fa fa-home text-primary text-xl mr-2"></i>
            <span class="font-bold text-lg">管理员首页</span>
        </a>
    </c:if>
    <c:if test="${person.level == 0}">
        <a href="${pageContext.request.contextPath}/person/index" class="flex items-center">
            <i class="fa fa-home text-primary text-xl mr-2"></i>
            <span class="font-bold text-lg">用户首页</span>
        </a>
    </c:if>

    <div class="ml-auto flex items-center">
        <c:if test="${person == null}">
            <a href="${pageContext.request.contextPath}/views/login.jsp"><i class="fa fa-user-circle mr-2"></i>请先登录</a>
        </c:if>
        <c:if test="${person != null}">
            <a href="${pageContext.request.contextPath}/admin/person/editPerson?pid=${person.pid}"><i
                    class="fa fa-user-circle mr-2"></i>${person.username}</a>
        </c:if>

        <a href="javascript:void(0);" onclick="logout()">退出登录</a>
    </div>
</nav>
<div class="admin-container">
    <div class="layui-tab" lay-filter="adminTab">
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <div class="module-header">
                    <div class="module-title">用户查询</div>
                    <div class="module-actions">
                        <button id="searchBtn"><i class="fa fa-search"></i> 查询</button>
                        <button id="resetBtn" class="reset-btn"><i class="fa fa-refresh"></i> 重置</button>
                    </div>
                </div>

                <!-- 查询表单 -->
                <div class="search-form">
                    <form class="layui-form" lay-filter="searchForm">
                        <div class="form-row">
                            <div class="form-group">
                                <label for="userId">用户ID</label>
                                <input type="text" id="userId" name="userId" placeholder="请输入用户ID"
                                       class="layui-input">
                            </div>
                            <div class="form-group">
                                <label for="username">用户名</label>
                                <input type="text" id="username" name="username" placeholder="请输入用户名"
                                       class="layui-input">
                            </div>
                            <div class="form-group">
                                <label for="email">邮箱</label>
                                <input type="text" id="email" name="email" placeholder="请输入邮箱地址"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="form-row">
                            <div class="form-group">
                                <label>创建时间范围</label>
                                <div class="date-range">
                                    <input type="text" id="startDate" name="startDate" placeholder="开始日期"
                                           class="layui-input">
                                    <span class="date-separator">至</span>
                                    <input type="text" id="endDate" name="endDate" placeholder="结束日期"
                                           class="layui-input">
                                </div>
                            </div>
                        </div>
                    </form>
                </div>

                <!-- 查询结果表格 -->
                <table class="admin-table">
                    <thead>
                    <tr>
                        <th>用户ID</th>
                        <th>用户名</th>
                        <th>邮箱</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="userTableBody">
                    <!-- 这里将通过JS动态填充查询结果 -->
                    <tr>
                        <td colspan="5" style="text-align: center; color: #999; padding: 40px 0;">
                            请输入查询条件并点击查询按钮
                        </td>
                    </tr>
                    </tbody>
                </table>

                <!-- 分页控件 -->
                <%--                <div class="pagination-container">--%>
                <%--                    <ul class="pagination">--%>
                <%--                        <li><a href="javascript:void(0);">上一页</a></li>--%>
                <%--                        <li class="active"><a href="javascript:void(0);">1</a></li>--%>
                <%--                        <li><a href="javascript:void(0);">2</a></li>--%>
                <%--                        <li><a href="javascript:void(0);">3</a></li>--%>
                <%--                        <li><a href="javascript:void(0);">下一页</a></li>--%>
                <%--                    </ul>--%>
                <%--                </div>--%>
            </div>
        </div>
    </div>
    <p style="color: red;">${findPersonMsg}</p>
</div>

<script>
    layui.use(['form', 'layer', 'element', 'laydate'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var element = layui.element;
        var laydate = layui.laydate;

        // 初始化日期选择器
        laydate.render({
            elem: '#startDate',
            format: 'yyyy-MM-dd',
            theme: '#1E9FFF'
        });

        laydate.render({
            elem: '#endDate',
            format: 'yyyy-MM-dd',
            theme: '#1E9FFF'
        });


        // 查询按钮事件
        document.getElementById('searchBtn').addEventListener('click', function () {
            // 获取表单数据
            var pid = document.getElementById('userId').value;
            var username = document.getElementById('username').value;
            var email = document.getElementById('email').value;
            var beginDate = document.getElementById('startDate').value;
            var endDate = document.getElementById('endDate').value;

            // 简单验证日期范围
            if (beginDate && endDate && beginDate > endDate) {
                layer.msg('开始日期不能晚于结束日期', {icon: 2});
                return;
            }

            // 模拟查询请求
            // layer.load(2);
            $.ajax({
                url: '${pageContext.request.contextPath}/admin/person/find',
                type: 'post',
                dataType: 'json',
                data: {
                    pid: pid,
                    username: username,
                    email: email,
                    beginDate: beginDate,
                    endDate: endDate
                }, success: function (res) {
                    if (res.length === 0) {
                        layer.msg('没有找到相关记录', {icon: 2});
                        $('#userTableBody').html(`
        <tr>
            <td colspan="5" style="text-align: center; color: #999; padding: 40px 0;">没有找到相关记录</td>
        </tr>
    `);
                    } else {
                        var tableBody = $('#userTableBody');
                        let html = '';
                        for (let i = 0; i < res.length; i++) {
                            html += `<tr>
                                <td>` + res[i].pid + `</td>
                                <td>` + res[i].username + `</td>
                                <td>` + res[i].email + `</td>
                                <td>` + formatTimestamp(res[i].date) + `</td>
                                <td>
                                    <div class="action-buttons">
                                        <button onclick="updatePerson(` + res[i].pid + `)" class="edit-btn"><i class="fa fa-pencil"></i> 编辑</button>
                                        <button onclick="delPerson(` + res[i].pid + `)" class="delete-btn"><i class="fa fa-trash"></i> 删除</button>
                                    </div>
                                </td>
                            </tr>`
                        }
                        tableBody.html(html)
                        // layer.closeAll('loading');
                    }
                }, error: function (res) {
                    layer.msg('查询失败', {icon: 2});
                    // layer.closeAll('loading');
                }
            })
        });

        // 重置按钮事件
        document.getElementById('resetBtn').addEventListener('click', function () {
            $('#userId').val('');
            $('#username').val('');
            $('#email').val('');
            $('#startDate').val('');
            $('#endDate').val('');
            layer.msg('已重置查询条件', {icon: 1});
            document.getElementById('userTableBody').innerHTML = `
                <tr>
                    <td colspan="5" style="text-align: center; color: #999; padding: 40px 0;">请输入查询条件并点击查询按钮</td>
                </tr>
            `;
        });

        // 操作按钮事件
        document.querySelectorAll('.action-buttons button').forEach(button => {
            button.addEventListener('click', function () {
                const action = this.textContent.trim();
                const userId = this.closest('tr').querySelector('td:first-child').textContent;
                layer.msg(`用户ID ${userId}：${action}功能已触发`, {icon: 1});
            });
        });

        // 选项卡切换事件
        element.on('tab(adminTab)', function (data) {
            var tabTitle = this.textContent;
            layer.msg(`切换到${tabTitle}模块`, {icon: 1});
        });
    });

    function formatTimestamp(timestamp) {
        const date = new Date(timestamp);
        console.log(date)
        const year = date.getFullYear();
        console.log('year', year)
        // 月份从 0 开始，+1 后补 0
        const month = String(date.getMonth() + 1).padStart(2, '0');
        console.log('month', month)
        const day = String(date.getDate()).padStart(2, '0');
        console.log('day', day)
        return (year + '-' + month + '-' + day);
    }

    //删除人员
    function delPerson(pid) {
        layer.confirm('确定要删除这个人员吗？', {
            icon: 3,
            title: '提示'
        }, function (index) {
            // 这里添加退出登录的实际逻辑
            layer.msg('已成功删除', {icon: 1});
            setTimeout(function () {
                // window.location.href = '/VoingSystem/admin/topic/delTopic?tid=' + tid;
                window.location.href = '/VoingSystem/admin/person/delPerson?pid=' + pid;
            }, 1500);
            layer.close(index);
        })
    }

    //修改人员
    function updatePerson(pid) {
        window.location.href = '/VoingSystem/admin/person/editPerson?pid=' + pid;
    }

    function logout() {
        layer.confirm('确定要退出登录吗？', {
            icon: 3,
            title: '提示'
        }, function (index) {
            // 这里添加退出登录的实际逻辑
            layer.msg('已成功退出登录', {icon: 1});
            setTimeout(function () {
                window.location.href = '/VoingSystem/logout';
            }, 1500);
            layer.close(index);
        })
    }
</script>
</body>
</html>